﻿using System.Collections.Generic;
using Fortune.Attributes.API;
using Fortune.Context;
using Fortune.DAO;
using Fortune.POCO.DO;
using Fortune.Util;

namespace Fortune.API
{
    /// <summary>
    /// 获取游戏当前全部文本数据（不从数据库查询）
    /// </summary>
    [Get("/proto/string/_realTime")]
    // ReSharper disable once ClassNeverInstantiated.Global
    public class StringProtoApi : AbstractApi<string>
    {
        protected override object handle(string param)
        {
            return ProtoUtil.toDataObject(LDB.strings.dataArray);
        }
    }
    
    /// <summary>
    /// 获取数据库中文本数据，分页查询
    /// </summary>
    [Get("/proto/string/_search")]
    public class GetStringProtos : AbstractApi<object>
    {
        private readonly StringProtoRepository stringProtoRepository =
            FortuneContext.getBean<StringProtoRepository>();

        protected override object handle(object param)
        {
            List<StringDO> ret = stringProtoRepository
                .Select
                .WhereIf(true, "name like '%@name%'", new {name="test Name"})
                .WhereIf(true, "zhCN like '%@zhCN%'", new {zhCN = "1234"})
                .ToList();

            return ret;
        }
    }
    
    /// <summary>
    /// 初始化文本数据库，会删除当前数据库并重新载入数据
    /// </summary>
    [Post("/proto/string/_init")]
    public class InitStringProtos : AbstractApi<object>
    {

        private StringProtoRepository stringProtoRepository =
            FortuneContext.getBean<StringProtoRepository>();
        
        protected override object handle(object param)
        {
            return true;
        }

        protected override void afterHandle(object param)
        {
            FortuneContext.fortuneBepInExConfig.isStringProtoStored.Value = true;
        }
    }
}